/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.cloud.business.usercenter.service.impl;

import com.github.pagehelper.Page;
import com.lvyh.lightframe.cloud.business.usercenter.bean.UcSysRoleBean;
import com.lvyh.lightframe.cloud.business.usercenter.bean.UcSysRoleMenuBean;
import com.lvyh.lightframe.cloud.business.usercenter.bean.UcSysRoleUserBean;
import com.lvyh.lightframe.cloud.business.usercenter.service.SysRoleService;
import com.lvyh.lightframe.cloud.business.usercenter.service.UserCenterMainDataService;
import com.lvyh.lightframe.cloud.common.model.PageResult;
import com.lvyh.lightframe.cloud.common.model.Result;
import com.lvyh.lightframe.cloud.common.model.SysRole;
import com.lvyh.lightframe.cloud.common.pagination.DefaultPageResult;
import com.lvyh.lightframe.cloud.common.pagination.PageParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private UserCenterMainDataService userCenterMainDataService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveRole(SysRole sysRole) throws Exception {
        String roleCode = sysRole.getCode();
        List<UcSysRoleBean> roleBeans = getSysRoleList(roleCode);
        if (!CollectionUtils.isEmpty(roleBeans)) {
            throw new Exception("角色code已存在");
        }
        UcSysRoleBean sysRoleBean = new UcSysRoleBean();
        BeanUtils.copyProperties(sysRole, sysRoleBean);
        userCenterMainDataService.saveUcSysRole(sysRoleBean);
    }

    private List<UcSysRoleBean> getSysRoleList(String code) throws Exception {
        UcSysRoleBean condition = new UcSysRoleBean();
        condition.setCode(code);
        return userCenterMainDataService.selectUcSysRole(condition);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteRole(Integer id) throws Exception {
        userCenterMainDataService.deleteUcSysRole(id);

        UcSysRoleMenuBean condition = new UcSysRoleMenuBean();
        condition.setRoleId(id);
        List<UcSysRoleMenuBean> sysRoleMenuList = userCenterMainDataService.selectUcSysRoleMenu(condition);
        if (!CollectionUtils.isEmpty(sysRoleMenuList)) {
            for (UcSysRoleMenuBean sysRoleMenuBean : sysRoleMenuList) {
                userCenterMainDataService.deleteUcSysRoleMenu(sysRoleMenuBean.getId());
            }
        }

        List<UcSysRoleUserBean> sysRoleUserList = getSysRoleList(id);
        if (!CollectionUtils.isEmpty(sysRoleUserList)) {
            for (UcSysRoleUserBean sysRoleUserBean : sysRoleUserList) {
                userCenterMainDataService.deleteUcSysRoleUser(sysRoleUserBean.getId());
            }
        }
    }

    private List<UcSysRoleUserBean> getSysRoleList(Integer id) throws Exception {
        UcSysRoleUserBean condition = new UcSysRoleUserBean();
        condition.setRoleId(id);
        return userCenterMainDataService.selectUcSysRoleUser(condition);
    }

    @Override
    public PageResult<SysRole> findRoles(Map<String, Object> params) throws Exception {
        Integer curPage = MapUtils.getInteger(params, "page");
        Integer limit = MapUtils.getInteger(params, "limit");
        Page<SysRole> page = new Page<>(curPage == null ? 0 : curPage, limit == null ? -1 : limit);

        UcSysRoleBean condition = new UcSysRoleBean();
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(page.getPageNum());
        pageParam.setPageSize(page.getPageSize());
        DefaultPageResult<UcSysRoleBean> pageInfo = userCenterMainDataService.selectUcSysRoleByCondition(condition, pageParam);

        List<UcSysRoleBean> sysRoleList = pageInfo.getResultList();
        List<SysRole> result = convert(sysRoleList);
        return PageResult.<SysRole>builder().data(result).code(0).count(pageInfo.getTotalCount()).build();
    }

    @Override
    @Transactional
    public Result saveOrUpdateRole(SysRole sysRole) throws Exception {
        if (sysRole.getId() == null) {
            this.saveRole(sysRole);
        } else {
            UcSysRoleBean condition = new UcSysRoleBean();
            condition.setId(sysRole.getId());
            List<UcSysRoleBean> sysRoleList = userCenterMainDataService.selectUcSysRole(condition);
            if (!CollectionUtils.isEmpty(sysRoleList)) {
                UcSysRoleBean sysRoleBean = sysRoleList.get(0);
                BeanUtils.copyProperties(sysRole, sysRoleBean);
                userCenterMainDataService.updateUcSysRole(sysRoleBean);
            }
        }
        return Result.succeed("操作成功");
    }

    @Override
    public List<SysRole> findAll() throws Exception {
        UcSysRoleBean condition = new UcSysRoleBean();
        List<UcSysRoleBean> sysRoleList = userCenterMainDataService.selectUcSysRole(condition);

        List<SysRole> result = convert(sysRoleList);
        return result;
    }


    public List<SysRole> convert(List<UcSysRoleBean> sysRoleList) {
        List<SysRole> result = new ArrayList<>();
        for (UcSysRoleBean bean : sysRoleList) {
            SysRole sysRole = new SysRole();
            BeanUtils.copyProperties(bean, sysRole);
            result.add(sysRole);
        }
        return result;
    }
}
